package com.frank.http.expand;

import java.util.Map;

import junit.runner.Version;

import com.frank.http.encryp.EncrypException;
import com.google.gson.Gson;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.util.Log;

public class PreferenceAppService {
	private final static String TAG = PreferenceAppService.class
			.getSimpleName();
	public final static String HTTP_DATA = "http_data";
	private static PreferenceAppService preferenceAppService;
	private Context context;

	private PreferenceAppService(Context context) {
		this.context = context;
	}

	public static synchronized void init(Context cxt) {
		if (preferenceAppService == null) {
			preferenceAppService = new PreferenceAppService(cxt);
		}
	}

	/**
	 * 单例模式，获取instance实例
	 * 
	 * @return
	 */
	public static PreferenceAppService getInstance() {
		if (preferenceAppService == null) {
			try {
				throw new EncrypException("please init first!");
			} catch (EncrypException e) {
				e.printStackTrace();
			}
		}
		return preferenceAppService;
	}

	/**
	 * 保存item
	 * 
	 * @param key
	 * @param value
	 */
	public void setItemAndroid(String key, String value) {
		if (value == null) {
			return;
		}
		Gson gson = new Gson();
		value = gson.toJson(value);
		Map<String, String> params = null;
		SharedPreferences preferences = context.getSharedPreferences(HTTP_DATA,
				Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) {
			LocalEncrpUtils localEncrpUtils = new LocalEncrpUtils();
			localEncrpUtils.init(0);
			try {
				key = localEncrpUtils.encryptBase64String(key);
				value = localEncrpUtils.encryptBase64String(value);
			} catch (Exception e) {
				e.printStackTrace();
			}
			editor.putString(key, value);
		} else {
			editor.putString(key, value);
		}
		params = getALL();
		if (null == params) {
			editor.commit();
			return;
		}
		for (String paramKey : params.keySet()) {
			if (!paramKey.equals(key)) {
				editor.putString(paramKey, params.get(paramKey));
			}
		}
		editor.commit();
	}

	/**
	 * 保存item
	 * 
	 * @param key
	 * @param value
	 */
	public void setItem(String key, String value) {
		Map<String, String> params = null;
		SharedPreferences preferences = context.getSharedPreferences(HTTP_DATA,
				Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) {
			LocalEncrpUtils localEncrpUtils = new LocalEncrpUtils();
			localEncrpUtils.init(0);
			try {
				key = localEncrpUtils.encryptBase64String(key);
				value = localEncrpUtils.encryptBase64String(value);
			} catch (Exception e) {
				e.printStackTrace();
			}
			editor.putString(key, value);
		} else {
			editor.putString(key, value);
		}
		params = getALL();
		if (null == params) {
			editor.commit();
			return;
		}
		for (String paramKey : params.keySet()) {
			if (!paramKey.equals(key)) {
				editor.putString(paramKey, params.get(paramKey));
			}
		}
		editor.commit();
	}

	/**
	 * 获得item
	 * 
	 * @param key
	 * @return
	 */
	public String getItemAndroid(String key) {
		String value = "0";
		Map<String, String> params = null;
		params = getALL();
		if (null == params) {
			Gson gson = new Gson();
			value = gson.fromJson(value, String.class);
			return value;
		}
		if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) {
			LocalEncrpUtils localEncrpUtils = new LocalEncrpUtils();
			localEncrpUtils.init(0);
			try {
				key = localEncrpUtils.encryptBase64String(key);
				if (params.get(key) != null) {
					value = params.get(key);
					value = localEncrpUtils.decryptBase64String(value);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			if (params.get(key) != null) {
				value = params.get(key);
			}
		}
		Gson gson = new Gson();
		value = gson.fromJson(value, String.class);
		return value;
	}

	/**
	 * 获得item
	 * 
	 * @param key
	 * @return
	 */
	public String getItem(String key) {
		String value = "0";
		Map<String, String> params = null;
		params = getALL();
		if (null == params) {
			return value;
		}
		if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) {
			LocalEncrpUtils localEncrpUtils = new LocalEncrpUtils();
			localEncrpUtils.init(0);
			try {
				key = localEncrpUtils.encryptBase64String(key);
				if (params.get(key) != null) {
					value = params.get(key);
					value = localEncrpUtils.decryptBase64String(value);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			if (params.get(key) != null) {
				value = params.get(key);
			}
		}
		return value;
	}

	/**
	 * 删除item
	 * 
	 * @param key
	 */
	public boolean removeItem(String key) {
		boolean flag = false;
		Map<String, String> params = null;
		SharedPreferences preferences = context.getSharedPreferences(HTTP_DATA,
				Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		params = getALL();
		if (null == params) {
			return flag;
		}
		if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) {
			LocalEncrpUtils localEncrpUtils = new LocalEncrpUtils();
			localEncrpUtils.init(0);
			try {
				key = localEncrpUtils.encryptBase64String(key);
				for (String paramKey : params.keySet()) {
					if (paramKey.equals(key)) {
						editor.remove(key);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			for (String paramKey : params.keySet()) {
				if (paramKey.equals(key)) {
					editor.remove(key);
				}
			}
		}
		editor.commit();
		return flag;
	}

	/**
	 * 清除item
	 * 
	 * @param key
	 * @param value
	 */
	public boolean clearAllItem() {
		boolean flag = false;
		SharedPreferences preferences = context.getSharedPreferences(HTTP_DATA,
				Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.clear();
		editor.commit();
		return flag;
	}

	/**
	 * 获取token，userId，loginId,password
	 * 
	 * @return
	 */
	public Map<String, String> getALL() {
		Map<String, String> params = null;
		try {
			SharedPreferences preferences = context.getSharedPreferences(
					HTTP_DATA, Context.MODE_PRIVATE);
			params = (Map<String, String>) preferences.getAll();
		} catch (Exception e) {
			Log.i(TAG, "数据为空");
		}
		return params;
	}

}
